package com.hmall.order.ma;

import com.hmall.common.FeignClientItem;
import com.hmall.order.config.TreadLocalMain;
import com.hmall.order.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class ListenRabbitdas {
    @Autowired
    private FeignClientItem feignClientItem;
    @Autowired
    private OrderMapper orderMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dl.ttl.queue", durable = "true"),
            exchange = @Exchange(name = "dl.ttl.direct"),
            key = "ttl"
    ))
    public void listenDlQueue(Map map)  {
        log.info("接收到 dl.ttl.queue的延迟消息：{}", map);
        log.error("用户未支付!");
        Long id = (Long) map.get("id");
        Long num = (Long) map.get("num");
        Long orderId = (Long) map.get("orderId");
        num=0-num;
        String msgId = (String) map.get("spring_returned_message_correlation");

        System.out.println(msgId);
        Long itemId = (Long) map.get("ItemId");
        TreadLocalMain.setThreadLocal(id.toString());
        log.info("订单取消"+orderId);
        orderMapper.deleteById(orderId);
        feignClientItem.Stock(itemId,Integer.parseInt(num.toString()));


    }
}
